#!/bin/sh

# Script to compare functions and instructions used by different igen models.
# Copyright (C) 2002-2024 Free Software Foundation, Inc.
# Contributed by Chris Demetriou of Broadcom Corporation (SiByte).
#
# This file is part of GDB, the GNU debugger.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This is a simple-minded script to compare the functions and instructions
# listed for two different models in one or more .igen files.
#
# It was intended to be useful to help factor models into common subsets.
#
# Things to note:
#
#    * igen include directives are not processed!
#
#    * functions and instructions with multiple definitions (e.g., based
#      on model names) are treated as being different.  In other words,
#      if two models have different functions named 'foo', this
#      script will say that one has one of the function definitions, and
#      the other has the other.

if [ "$#" -lt 2 ]; then
	echo "usage: $0 model1 model2 [file ...]" 1>&2
	exit 1
fi
model1="$1"
model2="$2"
shift; shift

gawk -v model1="$model1" -v model2="$model2" -F: -- '
BEGIN {
	thang_count = 0
}
function thang_has_model(t, m) {
#	printf("thang_has_model(%s, %s) (@ %s:%d)\n", t, m,
#	       thangs[t,"file"], thangs[t,"line"]);
	if (thangs[t,"nmodels"] == 0) return 1;

	for (j = 0; j < thangs[t,"nmodels"]; j++) {
#		printf("\tmodel \"%s\"\n", thangs[t,"models",j]);
		if (thangs[t,"models",j] == m) return 1;
	}
#	printf("\t-> 0\n");
	return 0
}
function compare_models(m1, m2) {
#	printf("compare_models(%s, %s)\n", m1, m2);
	seen_any=0
	for (i = 0; i < thang_count; i++) {
		if (thang_has_model(i, m1) && !thang_has_model(i, m2)) {
			if (!seen_any) {
				printf("Things in %s but not in %s:\n", m1, m2);
				seen_any = 1
			}
			printf("%s:%d: %s\n", thangs[i,"file"],
			       thangs[i,"line"], thangs[i,"contents"]);
		}
	}
}
$0 ~ /^:/ && $2 == "model" {
	# ignore.
	# print "model " $0
}
($0 ~ /^:/ && $2 == "function") || \
($0 ~ /^:/ && $2 == "internal") || \
($0 ~ /^[0-9]/) {
	# a function, internal, or instruction.

	current_thang = thang_count
	thang_count++

	thangs[current_thang,"file"] = FILENAME
	thangs[current_thang,"line"] = NR
	thangs[current_thang,"contents"] = $0
	thangs[current_thang,"nmodels"] = 0

	if ($0 ~ /^:/) {
		thangs[current_thang,"type"] = $2
	} else {
		thangs[current_thang,"type"] = "instruction"
	}
}
$0 ~ /^\*/ {
	split(substr($1, 2), tmp_models, /,/)
	for (key in tmp_models) {
		current_model = thangs[current_thang,"nmodels"]
		thangs[current_thang,"nmodels"]++
		thangs[current_thang,"models",current_model] = tmp_models[key]
	}
}
END {
	compare_models(model1, model2)
	if (seen_any) printf("\n");
	compare_models(model2, model1)
}' "$@"

exit "$?"
